package TTEST;

import java.io.File;
import java.io.IOException;

import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import TTEST.SwingShell.TerminalDialog;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

public class _ConnectionThread extends Thread
{
	String hostname;
	String username;
	String pa;
	public _ConnectionThread(LabWindow ref,JTextArea v,String hostname, String username,String pass)
	{
		_window=ref;
		this.pa=pass;
		this.aaa=v;
		this.hostname = hostname;
		this.username = username;
	}
	public _ConnectionThread(JTextArea v,String hostname, String username,String pass)
	{
		this.pa=pass;
		this.aaa=v;
		this.hostname = hostname;
		this.username = username;
	}
	public _ConnectionThread(JTextArea v,String hostname, String username)
	{
		this.aaa=v;
		this.hostname = hostname;
		this.username = username;
	}
	public _ConnectionThread(String hostname, String username)
	{
		this.hostname = hostname;
		this.username = username;
	}

	public void run()
	{
		Connection conn = new Connection(hostname);
		try
		{
			String[] hostkeyAlgos = SwingShell.database.getPreferredServerHostkeyAlgorithmOrder(hostname);
			if (hostkeyAlgos != null)
				conn.setServerHostKeyAlgorithms(hostkeyAlgos);

			conn.connect(new _AdvancedVerifier());
			boolean 
				enableKeyboardInteractive = true,enableDSA = true,enableRSA = true;
			String lastError = null;
			while (true)
			{
				if ((enableDSA || enableRSA) && conn.isAuthMethodAvailable(username, "publickey"))
				{
					if (enableDSA)
					{
						File key = new File(SwingShell.idDSAPath);

						if (key.exists())
						{
							_EnterSomethingDialog esd = new _EnterSomethingDialog(SwingShell.loginFrame, "DSA Authentication",
									new String[] { lastError, "Enter DSA private key password:" }, true);
							esd.setVisible(true);

							boolean res = conn.authenticateWithPublicKey(username, key, esd.answer);

							if (res == true)
								break;

							lastError = "DSA authentication failed.";
						}
						enableDSA = false; // do not try again
					}

					if (enableRSA)
					{
						File key = new File(SwingShell.idRSAPath);

						if (key.exists())
						{
							_EnterSomethingDialog esd = new _EnterSomethingDialog(SwingShell.loginFrame, "RSA Authentication",
									new String[] { lastError, "Enter RSA private key password:" }, true);
							esd.setVisible(true);
							boolean res = conn.authenticateWithPublicKey(username, key, esd.answer);

							if (res == true)
								break;

							lastError = "RSA authentication failed.";
						}
						enableRSA = false; // do not try again
					}

					continue;
				}
				if (conn.isAuthMethodAvailable(username, "password"))
				{
					boolean res = conn.authenticateWithPassword(username, (pa==null)?"log":pa);
					if (res == true)
						break;
					lastError = "Password authentication failed."; // try again, if possible
					continue;
				}
				throw new IOException("No supported authentication methods available.");
			}
			Session sess = conn.openSession();
			int x_width = 400;
			int y_width = 40;
			sess.requestPTY("dumb", x_width, y_width, 0, 0, null);
			sess.startShell();
			System.out.println("polaczenie ok");
			td = new _TerminalDialog(_window,aaa,username + "@" + hostname, sess, x_width, y_width);
		}
		catch (IOException e)
		{
			JOptionPane.showMessageDialog(SwingShell.loginFrame, "Exception: " + e.getMessage());
		}
	}
	JTextArea aaa;
	_TerminalDialog td;
	LabWindow _window;
}